package com.spring.crm.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.crm.conf.CommonResult;
import com.spring.crm.conf.ResultCode;
import com.spring.crm.entity.Reimburse;
import com.spring.crm.mapper.ReimburseMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何卓
 * @since 2022-01-18
 */
@RestController
@RequestMapping("/api/crm/reimburse")
public class ReimburseController {
    @Resource
    private ReimburseMapper reimburseMapper;

    @CrossOrigin(origins = "*")
    @PostMapping("insert")
    public CommonResult insertOne(@RequestBody Reimburse record) {
        CommonResult rs;
        try {
            if(record.getUserId()==null){
                return CommonResult.failed("员工不能为空");
            }
            LocalDateTime dd = LocalDateTime.now(); // 注意是util包下的Date
            record.setCreateTime(dd.toString());
            record.setUpdateTime(dd.toString());
            int ret = reimburseMapper.insert(record);
            if (ret != 1){
                rs = CommonResult.failed();
            }else {
                JSONObject data = new JSONObject();
                data.put("reimburseId",record.getReimburseId());
                rs = CommonResult.success(data);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @GetMapping("condition")
    public CommonResult selectByCondition(@RequestParam Map<String,String> map) {
        CommonResult rs;
        try {
            QueryWrapper<Reimburse> queryWrapper = new QueryWrapper<>();
            if(map.containsKey("reimburseId")){
                queryWrapper.eq("reimburse_id",map.get("reimburseId"));
            }
            if(map.containsKey("userId")){
                queryWrapper.eq("user_id",map.get("userId"));
            }
            if(map.containsKey("reimburseStatus")){
                queryWrapper.eq("reimburse_status",map.get("reimburseStatus"));
            }
            if(map.containsKey("salesId")){
                queryWrapper.eq("sales_id",map.get("salesId"));
            }
            queryWrapper.orderByDesc("create_time");
            Page<Reimburse> page = new Page<>(Long.valueOf(map.get("current")),Long.valueOf(map.get("size")));
            IPage<Reimburse> SalesIPage = reimburseMapper.selectPage(page,queryWrapper);
            Integer selectCount = Math.toIntExact(reimburseMapper.selectCount(queryWrapper));
            List<Reimburse> list = SalesIPage.getRecords();
            rs = CommonResult.success(list,selectCount);
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @DeleteMapping ("delete")
    public CommonResult deleteOne(@RequestBody Reimburse record) {
        CommonResult rs;
        try {
            Reimburse reimburse = reimburseMapper.selectById(record.getReimburseId());
            if(reimburse==null){
                return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
            }
            int ret = reimburseMapper.deleteById(record.getReimburseId());
            if (ret != 1) {
                rs = CommonResult.failed();
            } else {
                rs = CommonResult.success("");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @PutMapping("update")
    public CommonResult updateOne(@RequestBody Reimburse record) {
        CommonResult rs;
        try {
            Reimburse reimburse = reimburseMapper.selectById(record.getReimburseId());
            if(reimburse==null){
                return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
            }
            LocalDateTime dd = LocalDateTime.now(); // 注意是util包下的Date
            record.setUpdateTime(dd.toString());
            int ret = reimburseMapper.updateById(record);
            if (ret != 1) {
                rs = CommonResult.failed();
            } else {
                JSONObject data = new JSONObject();
                data.put("reimburseId", record.getReimburseId());
                rs = CommonResult.success(data);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }
}
